package netty.pastePack;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;
import netty.pastePack.protocol.MessageProtocol;

import java.util.List;

/**
 * @Description TODO
 * @Author 李龙平
 * @Date 2020/10/14 14:25
 */
public class MyMessageDecoder extends ReplayingDecoder<Void> {

    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf in, List<Object> list) throws Exception {
        System.out.println("解码方法调用。。。");
        //将得到的二进制转成 MessageProtocol对象
        int len = in.readInt();
        byte[] bytes = new byte[len];
        in.readBytes(bytes);

        //封装MessageProtocol对象对象n
        MessageProtocol messageProtocol = new MessageProtocol();
        messageProtocol.setLen(len);
        messageProtocol.setContent(bytes);

        list.add(messageProtocol);
    }
}
